#include <iostream>  
  
using namespace std;  
  
// 定义Node   
class Node {  
public:  
    int val;  
    Node* next;  
    Node(int x) : val(x), next(NULL) {}  
};  
  
// 定义LinkedList类  
class LinkedList {  
public:  
    Node* head;  
    LinkedList() : head(NULL) {}  
  
    // 添加节点  
    void add(int val) {  
        Node* newNode = new Node(val);  
        newNode->next = head;  
        head = newNode;  
    }  
  
    // 打印链表  
    void print() {  
        Node* cur = head;  
        while (cur) {  
            cout << cur->val << " ";  
            cur = cur->next;  
        }  
        cout << endl;  
    }  
  
    // 按照基准进行划分  
    LinkedList* partition(int base) {  
        LinkedList* smallerHead = NULL;  
        LinkedList* smallerTail = NULL;  
        LinkedList* greaterHead = NULL;  
        LinkedList* greaterTail = NULL;  
        Node* cur = head;  
  
        while (cur) {  
            if (cur->val < base) {  
                if (smallerHead == NULL) {  
                    smallerHead = new LinkedList();  
                    smallerTail = smallerHead;  
                }  
                smallerTail->add(cur->val);  
                smallerTail = smallerTail->next;  
            } else {  
                if (greaterHead == NULL) {  
                    greaterHead = new LinkedList();  
                    greaterTail = greaterHead;  
                }  
                greaterTail->add(cur->val);  
                greaterTail = greaterTail->next;  
            }  
            cur = cur->next;  
        }  
  
        return greaterHead == NULL ? smallerHead : greaterHead;  
    }  
};  
  
int main() {  
    LinkedList list;  
    list.add(1);  
    list.add(2);  
    list.add(3);  
    list.add(4);  
    list.add(5);  
    list.add(6);  
    list.add(7);  
    list.add(8);  
    list.add(9);  
    list.add(10);  
    cout << "Original list: ";  
    list.print();  
  
    LinkedList* newList = list.partition(5);  
    cout << "List after partition: ";  
    newList->print();  
  
    return 0;  
};